
clear
cap clear matrix
cap clear mata
set maxvar 100000


import delimited "data/raw/amountsMainLargeStakes.csv" , case(preserve) 
sort round
cap rename ïwin win
drop lose
save "data/amountsMainLargeStakes.dta", replace
clear



import delimited "data/raw/amountsRiskLargeStakes.csv" , case(preserve) 
rename RoundRisk roundRisk
rename Incentive win
rename Successprobability probability

// Generate dummy for information structure and signal received to match to main data

gen infoStr = 0 
replace infoStr = 1 if probability == "postStrPosSigGood" | probability == "postStrPosSigBad"  

gen signal = 0
replace signal = 1 if probability == "postStrPosSigGood" | probability == "postStrNegSigGood"  


save "data/amountsRiskLargeStakes.dta", replace
clear



import delimited "data/raw/largeStakesMain.tsv", case(preserve) delimiter(tab) varnames(1) bindquote(strict) encoding(UTF-16LE) 


// Initial data management
if 1 {


	drop catastropheTreatment

	// drop if RecipientEmail == ""	// Drop test-runs 
	drop if Duration == 0 // Drop observations in which someone attempted to log in from a mobile device. 

	gen durationInMinutes = Duration / 60



	keep if gender ~= . 
	rename gender male
	drop comCheck*

	gen id = _n

	forvalues r = 1 / 18 {
		rename r`r'_infoStr 			infoStr`r' 			
		rename r`r'_infoStr2 			infoStr2_`r' 			
		rename r`r'_bet 				bet`r' 			
		rename r`r'_belief 				belief`r' 			
		rename r`r'_beliefReverse		beliefReverse`r'	
		rename r`r'_tBetChoice_Page		tBetChoice`r'
		rename r`r'_tAdvisorChoice_Page	tAdvisorChoice`r'	
		
		replace belief`r' = beliefReverse`r' if belief`r' == . 
		replace infoStr`r' = infoStr2_`r' if infoStr`r' == . 			
	}



	drop beliefReverse*

	drop infoStr2_*


	drop state


	rename winLeft posSkewLeft
	drop winLeft*
	
	save "data/dataPreliminary.dta", replace
}



// Data management risk decisions
if 1 {

	forvalues r = 1 / 26 {
		split ceScramble`r', parse(",", "[", "]") gen(r`r'_ceValue)
		destring r`r'_ceValue*, force replace
		
		drop r`r'_ceValue1
		forvalues i = 2 / 12 {
			local iMinus = `i' - 1
			rename r`r'_ceValue`i' r`r'_ceValue`iMinus'
		}
		
	}
	

	forvalues r = 1 / 26 {
		forvalues i = 1 / 11 {
			rename r`r'_riskDelay`i' riskDelay`i'_`r'
			rename r`r'_risk`i' risk`i'_`r'
			rename r`r'_ceValue`i' ceValue`i'_`r'
		}
					
		forvalues i = 1 / 11 {
			replace risk`i'_`r' = riskDelay`i'_`r' if delayTreatment == 1	
		}
	}

	drop riskDelay* risk*Reverse* 

	keep id risk* delayTreatment postStr* prior ceValue*
	
	reshape long risk1_ risk2_ risk3_ risk4_ risk5_ risk6_ risk7_ risk8_ risk9_ risk10_ risk11_ ///
				 ceValue1_ ceValue2_ ceValue3_ ceValue4_ ceValue5_ ceValue6_ ceValue7_ ceValue8_ ceValue9_ ceValue10_ ceValue11_, i(id) j(round)

	
	forvalues i = 1 / 11 {
		rename risk`i'_ risk`i'
		rename ceValue`i'_ ceValue`i'
	}
	
	
	reshape long risk ceValue, i(id round) j(decision)
			
	sort id round ceValue
	
	rename risk riskChoice
	// The variable riskChoice equals 1 if the lottery is chosen and 2 if the certainty equivalent is chosen
	
	gen switchBack_i = riskChoice[_n] - riskChoice[_n-1] == -1
	replace switchBack_i = . if ceValue == -85
	bysort id round: egen switchBack = max(switchBack_i)
	
	bysort id: egen fracMultiSwitch = mean(switchBack)
	bysort id: egen everMultiSwitch = max(switchBack)
		
	replace riskChoice = riskChoice - 1
	// Now riskChoice equals 0 if the lottery is chosen and 1 if the certainty equivalent is chosen
	replace riskChoice = . if riskChoice == 0
	replace riskChoice = riskChoice * ceValue
	
	bysort id round: egen minCE = min(riskChoice)
	// minCE is the lowest certainty equivalent that is better than the lottery. 
	// Hence, it's an upper bound on the certainty equivalent. 
	
	// For subjects who select the lottery all the way through, the certainty equivalent must exceed 12. 
	// These subjects' data will show as missing. Replace it with 15. 
	replace minCE = 100 if minCE == .

	egen tagRisk = tag(id round) 
	
	keep if tagRisk == 1
	drop tagRisk
	drop decision riskChoice switchBack_i ceValue
	
	
	rename round roundRisk
	merge m:1 roundRisk using "data/amountsRiskLargeStakes.dta"
	drop _m
	sort id round
	
	
	
	gen winProb = .

	gen id2 = _n
	su id2 
	local max = r(max)
	
	forvalues i = 1 / `max' {
		
		local prob = probability[`i']
		su `prob' if id2 == `i'
		local probNum = r(mean)
		replace winProb = `probNum' if id2 == `i'
		
	}
	
	cap rename Catastrophe catastropheTreatment
	
	gen lose = abs(win - 100)
	replace lose = 100 if catastropheTreatment == 1
	gen ev = winProb * win - (1 - winProb) * lose
	
	cap gen riskAversion_i = ev - minCE if winProb ~= 1 & winProb ~= 0
	bysort id: egen riskAversion = mean(riskAversion_i)
	cap drop riskAversion_i
		
	cap gen riskAversionCat_i = ev - minCE if winProb ~= 1 & winProb ~= 0
	bysort id: egen riskAversionCat = mean(riskAversionCat_i)
	cap drop riskAversionCat_i
	
	cap gen riskAversionNonCat_i = ev - minCE if winProb ~= 1 & winProb ~= 0
	bysort id: egen riskAversionNonCat = mean(riskAversionNonCat_i)
	cap drop riskAversionNonCat_i
	
	
			
	// Certainty equivalent: use midpoint
	if 1 {	
					
		gen minCEmid = .
		replace minCEmid = -85   if round(minCE) == -85 
		replace minCEmid = -80   if round(minCE) == -75
		replace minCEmid = -62.5 if round(minCE) == -50 
		replace minCEmid = -37.5 if round(minCE) == -25
		replace minCEmid = -17.5 if round(minCE) == -15
		replace minCEmid = -7.5  if round(minCE) == 0 
		replace minCEmid = 7.5   if round(minCE) == 15  
		replace minCEmid = 17.5  if round(minCE) == 25  
		replace minCEmid = 37.5  if round(minCE) == 50  
		replace minCEmid = 62.5  if round(minCE) == 75  
		replace minCEmid = 80    if round(minCE) == 85  
		replace minCEmid = 85    if round(minCE) == 100  

		gen minCEhigh = minCE
		replace minCE = minCEmid
		drop minCEmid
		
	}
	
	
* ------------------------------------------------------------------------------		
		// Measuring amount of noise in case of sure outcomes
		if 1 {
		
			tab minCE if winProb == 1 & win == 70
			tab minCE if winProb == 1 & win == 30
			tab minCE if winProb == 0 & win == 70 & catastrophe == 0
			tab minCE if winProb == 0 & win == 30 & catastrophe == 0
			tab minCE if winProb == 0 & win == 70 & catastrophe == 1
			tab minCE if winProb == 0 & win == 30 & catastrophe == 1	
			
			su minCE if winProb == 1 & win == 70
			su minCE if winProb == 1 & win == 30
			su minCE if winProb == 0 & win == 70 & catastrophe == 0
			su minCE if winProb == 0 & win == 30 & catastrophe == 0
			su minCE if winProb == 0 & win == 70 & catastrophe == 1
			su minCE if winProb == 0 & win == 30 & catastrophe == 1	
				
			
		}
* ------------------------------------------------------------------------------		
	
	// Certainty equivalent for lotteries with sure outcome
	if 1 {
		
		
		
		
		// Extend dataset by the imputed observations
		count if round == 1
		local numSubj = r(N)
		
		count 
		local numObsOld = r(N)
		
		local numObsNew = `numObsOld' + 8*`numSubj'
		
		set obs `numObsNew'
		
		gen realObs = id ~= .
		sort realObs
		
		replace id = floor( (_n-1) / 8) + 1 if realObs == 0
		replace round = 28 + mod(_n, 8) + 1 if realObs == 0
		
		replace win 		=  20 if round == 29 
		replace winProb 	=  0 if round == 29 
		replace catastrophe =  0 if round == 29 
											 
		replace win 		=  20 if round == 30 
		replace winProb 	=  0 if round == 30 
		replace catastrophe =  1 if round == 30
											 
		replace win 		=  20 if round == 31 
		replace winProb 	=  1 if round == 31 
		replace catastrophe =  0 if round == 31 
											 
		replace win 		=  20 if round == 32 
		replace winProb 	=  1 if round == 32 
		replace catastrophe =  1 if round == 32 
											 
		replace win 		=  80 if round == 33 
		replace winProb 	=  0 if round == 33 
		replace catastrophe =  0 if round == 33 
											 
		replace win 		=  80 if round == 34 
		replace winProb 	=  0 if round == 34 
		replace catastrophe =  1 if round == 34 
		
		replace win 		=  80 if round == 35
		replace winProb 	=  1 if round == 35
		replace catastrophe =  0 if round == 35 
		
		replace win 		=  80 if round == 36 
		replace winProb 	=  1 if round == 36 
		replace catastrophe =  1 if round == 36  
		
		
		
		// I have the CEs for sure outcomes corresponding to incentive amounts 
		// 10 and 2. Hence, for incentive amounts 11 and 1, use the CEs for incentive
		// amounts 10 and 2, scaled appropriately. 
					
		
		gen sureLoseHighNonCat_i 	= minCE if round == 1
		gen sureWinHighNonCat_i 	= minCE if round == 2
		gen sureLoseLowNonCat_i 	= minCE if round == 3
		gen sureWinLowNonCat_i 		= minCE if round == 4
		gen sureLoseHighCat_i 		= minCE if round == 5
		gen sureWinHighCat_i 		= minCE if round == 6
		gen sureLoseLowCat_i 		= minCE if round == 7
		gen sureWinLowCat_i 		= minCE if round == 8
		
		
		bysort id: egen sureLoseHighNonCat 	= mean(sureLoseHighNonCat_i )
		bysort id: egen sureWinHighNonCat 	= mean(sureWinHighNonCat_i 	)
		bysort id: egen sureLoseLowNonCat 	= mean(sureLoseLowNonCat_i 	)
		bysort id: egen sureWinLowNonCat 	= mean(sureWinLowNonCat_i 	)
		bysort id: egen sureLoseHighCat 	= mean(sureLoseHighCat_i 	)
		bysort id: egen sureWinHighCat 		= mean(sureWinHighCat_i 	)
		bysort id: egen sureLoseLowCat 		= mean(sureLoseLowCat_i 	)
		bysort id: egen sureWinLowCat 		= mean(sureWinLowCat_i 		)
		
			
		bysort id: egen certainLoseHighNonCat 	= mean(sureLoseHighNonCat )
		bysort id: egen certainWinHighNonCat 	= mean(sureWinHighNonCat  )
		bysort id: egen certainLoseLowNonCat 	= mean(sureLoseLowNonCat  )
		bysort id: egen certainWinLowNonCat 	= mean(sureWinLowNonCat   )
		bysort id: egen certainLoseHighCat 		= mean(sureLoseHighCat 	  )
		bysort id: egen certainWinHighCat 		= mean(sureWinHighCat 	  )
		bysort id: egen certainLoseLowCat 		= mean(sureLoseLowCat 	  )
		bysort id: egen certainWinLowCat 		= mean(sureWinLowCat 	  )

		
		
		
		
		replace minCE = certainLoseHighNonCat  * (20/30)   if minCE == . & winProb == 0 & win == 80 & catastrophe == 0 
		replace minCE = certainWinHighNonCat   * (80/70)   if minCE == . & winProb == 1 & win == 80 & catastrophe == 0 
		replace minCE = certainLoseLowNonCat   * (80/70)   if minCE == . & winProb == 0 & win == 20 & catastrophe == 0 
		replace minCE = certainWinLowNonCat    * (20/30)   if minCE == . & winProb == 1 & win == 20 & catastrophe == 0 
		replace minCE = certainLoseHighCat     * (100/100) if minCE == . & winProb == 0 & win == 80 & catastrophe == 1 
		replace minCE = certainWinHighCat 	   * (80/70)   if minCE == . & winProb == 1 & win == 80 & catastrophe == 1 
		replace minCE = certainLoseLowCat 	   * (100/100) if minCE == . & winProb == 0 & win == 20 & catastrophe == 1 
		replace minCE = certainWinLowCat 	   * (20/30)   if minCE == . & winProb == 1 & win == 20 & catastrophe == 1 
		
		
		
	}
	
	
	save "data/riskSwitchPointsLargeStakesorig.dta", replace


	// Double up rounds 25 and 26 which correspond to the zero-incentive treatment
	// so they can be matched to both the catastrophe condition and the non-catastrophe condition
	
	keep if round == 25 | round == 26
	replace round = round + 2
	replace catastropheTr = 1

	append using "data/riskSwitchPointsLargeStakesorig.dta"
	save "data/riskSwitchPointsLargeStakes.dta", replace
	
	
	// Get smoothed certainty equivalents with ridge regression
	if 1 {
	
		use "data/riskSwitchPointsLargeStakes.dta", replace
		drop if round > 26
		
		rename minCE			v0_
		rename win 				v1_
		rename lose 			v2_
		rename winProb			v3_
		
		
		forvalues i0 = 0 / 3 {
			if `i0' > 0 {
				su v`i0'_ 
				replace v`i0'_ = (v`i0'_ - r(mean)) / r(sd)
			}
			forvalues i1 = 0 / 3 {
				gen v`i0'`i1'_ = v`i0'_ * v`i1'_
				su v`i0'`i1'_
				replace v`i0'`i1'_ = (v`i0'`i1'_ - r(mean)) / r(sd)
				forvalues i2 = 0 / 3 {
					gen v`i0'`i1'`i2'_ = v`i0'_ * v`i1'_ * v`i2'_
					su v`i0'`i1'`i2'_ 
					replace v`i0'`i1'`i2'_ = (v`i0'`i1'`i2'_ - r(mean)) / r(sd)
					forvalues i3 = 0 / 3 {
						gen v`i0'`i1'`i2'`i3'_ = v`i0'_ * v`i1'_ * v`i2'_ * v`i3'_							
						su v`i0'`i1'`i2'`i3'_
						replace v`i0'`i1'`i2'`i3'_ = (v`i0'`i1'`i2'`i3'_ - r(mean)) / r(sd)
					}
				}
			}			
		}
		
		keep minCE v*_ id roundRisk
		reshape wide minCE v*_ , i(id) j(roundRisk)
					
		forvalues r = 1 / 26 {
			
			noisily: di `r'
			preserve 
				rename v0_`r' depVar
				gen v0_`r' = .
				drop v*0*_`r'	// Drop all variables that include the dependent variable
				elasticnet linear depVar v*, alphas(0)  // Alpha is the weight on the lasso penalty, so alpha = 0 is ridge-regression
			restore

			predict depVarPred`r'
			
		}
		
		keep id depVarPred*
		reshape long depVarPred`r', i(id) j(roundRisk)
		rename depVarPred minCElasso
		
		save "data/riskSwitchPointsLassoLargeStakes.dta", replace

		// Double up rounds 25 and 26 which correspond to the zero-incentive treatment
		// so they can be matched to both the catastrophe condition and the non-catastrophe condition
		
		keep if round == 25 | round == 26
		replace round = round + 2

		append using "data/riskSwitchPointsLassoLargeStakes.dta"
		save "data/riskSwitchPointsLassoLargeStakes.dta", replace
	
	}
	
					
		
}


clear 
use "data/dataPreliminary.dta", replace

drop *risk*



reshape long infoStr bet belief signal order changeDecision clickCont state tBetChoice tAdvisorChoice, i(id) j(round) 

drop if round > 18

replace clickCont = 0 if clickCont == .

egen tag = tag(id)


merge m:1 round using "data/amountsMainLargeStakes.dta"
drop _m
cap rename Catastrophe catastropheTreatment

gen winProb = . 
replace winProb = 0 if infoStr == 1 & signal == 0
replace winProb = 1 if infoStr == 0 & signal == 1
replace winProb = 0.111 if prior == "low"  & infoStr == 0 & signal == 0
replace winProb = 0.333 if prior == "low"  & infoStr == 1 & signal == 1
replace winProb = 0.333 if prior == "mid"  & infoStr == 0 & signal == 0
replace winProb = 0.667 if prior == "mid"  & infoStr == 1 & signal == 1
replace winProb = 0.667 if prior == "high" & infoStr == 0 & signal == 0
replace winProb = 0.889 if prior == "high" & infoStr == 1 & signal == 1

merge m:1 id win winProb catastropheTreatment using "data/riskSwitchPointsLargeStakes.dta"
drop _m



merge m:1 id roundRisk using "data/riskSwitchPointsLassoLargeStakes.dta"


// Examine observations that are not merged
if 0 {
	order win winProb catastropheTreatment minCE
	sort minCE win winProb catastrophe
	
	count if win == 80 & winProb == 1 & catastrophe == 1 & minCE~= .
	count if win == 80 & winProb == 1 & catastrophe == 1 & minCE~= .
	count if win == 80 & winProb == 1 & catastrophe == 1 & minCE~= .
	count if win == 80 & winProb == 1 & catastrophe == 1 & minCE~= .

}




// Personality characteristics and demographics

if 1 {
	gen crt1correct = round(100*crt1) == 25
	gen crt2correct = crt2 == 10
	gen crt3correct = crt3 == 23
	gen crt4correct = crt4 == 2
	gen crt5correct = crt5 == 8
	gen crt6correct = crt6 == "Emily" | crt6 == "emily"
	gen crt7correct = crt7 == 1
	
	gen crtSum = crt1c + crt2c + crt3c + crt4c + crt5c + crt6c + crt7c
	
	gen conscientiousness = big5_4 - big5_5 + big5_6 - big5_7 - big5_8 + big5_9 + big5_10 + big5_11 - big5_12
	
	egen conscientiousnessRank = rank(conscientiousness)
	su conscientiousnessRank
	replace conscientiousnessRank = conscientiousnessRank / r(max)
	

	
	
	gen gpaAvgRecode = .
	replace gpaAvgRecode = 6 	if gpaAvg == 1
	replace gpaAvgRecode = 5.75 if gpaAvg == 2
	replace gpaAvgRecode = 5.5 	if gpaAvg == 3
	replace gpaAvgRecode = 5.25 if gpaAvg == 4
	replace gpaAvgRecode = 5 	if gpaAvg == 5
	replace gpaAvgRecode = 4.75 if gpaAvg == 6
	replace gpaAvgRecode = 4.5 	if gpaAvg == 7
	replace gpaAvgRecode = 4.25 if gpaAvg == 8
	replace gpaAvgRecode = 4 	if gpaAvg == 9
	replace gpaAvgRecode = . 	if gpaAvg == 12
	
	replace gpaAvg = gpaAvgRecode
	drop gpaAvgRecode
	
	
	gen gpaMathsRecode = . 
	replace gpaMathsRecode = 6 		if gpaMaths == 1
	replace gpaMathsRecode = 5.5 	if gpaMaths == 2
	replace gpaMathsRecode = 5 		if gpaMaths == 3
	replace gpaMathsRecode = 4.5 	if gpaMaths == 4
	replace gpaMathsRecode = 4 		if gpaMaths == 5
	replace gpaMathsRecode = 3.5 	if gpaMaths == 6
	replace gpaMathsRecode = 3 		if gpaMaths == 7
	replace gpaMathsRecode = 2.5 	if gpaMaths == 8
	replace gpaMathsRecode = 2 		if gpaMaths == 9
	replace gpaMathsRecode = 1.5 	if gpaMaths == 10
	replace gpaMathsRecode = 1 		if gpaMaths == 11
	replace gpaMathsRecode = . 		if gpaMaths == 12
		
	replace gpaMaths = gpaMathsRecode
	drop gpaMathsRecode
	
	
	gen gpaLanguageRecode = . 
	replace gpaLanguageRecode = 6 		if gpaLanguage == 1
	replace gpaLanguageRecode = 5.5 	if gpaLanguage == 2
	replace gpaLanguageRecode = 5 		if gpaLanguage == 3
	replace gpaLanguageRecode = 4.5 	if gpaLanguage == 4
	replace gpaLanguageRecode = 4 		if gpaLanguage == 5
	replace gpaLanguageRecode = 3.5 	if gpaLanguage == 6
	replace gpaLanguageRecode = 3 		if gpaLanguage == 7
	replace gpaLanguageRecode = 2.5 	if gpaLanguage == 8
	replace gpaLanguageRecode = 2 		if gpaLanguage == 9
	replace gpaLanguageRecode = 1.5 	if gpaLanguage == 10
	replace gpaLanguageRecode = 1 		if gpaLanguage == 11
	replace gpaLanguageRecode = 6 		if gpaLanguage == 12
		
	replace gpaLanguage = gpaLanguageRecode
	drop gpaLanguageRecode
	
	la de faculty 1 "Theology" 2 "Law" 3 "Econ and Business" 4 "Medicine" 5 "Vetsuisse" 6 "Philosophical" 7 "MatNat" 8 "ETH"
	la val faculty faculty
	
	gen bayesCorrect = 	strpos(bayes, "Bais") > 0 | ///
						strpos(bayes, "Baye") > 0 | ///
						strpos(bayes, "baye") > 0 | ///
						strpos(bayes, "Bedingt") > 0 | ///
						strpos(bayes, "bedingt") > 0 | ///
						strpos(bayes, "Conditional") > 0 | ///
						strpos(bayes, "conditional") > 0
	
}



save "data/dataLargeStakes.dta", replace









